package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IAboutUsService;
import com.mdd.admin.validate.AboutUsCreateValidate;
import com.mdd.admin.validate.AboutUsUpdateValidate;
import com.mdd.admin.validate.AboutUsSearchValidate;
import com.mdd.admin.vo.AboutUsListedVo;
import com.mdd.admin.vo.AboutUsDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.AboutUs;
import com.mdd.common.mapper.AboutUsMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 关于我们实现类
 * @author cxt
 */
@Service
public class AboutUsServiceImpl implements IAboutUsService {
        
    @Resource
    AboutUsMapper aboutUsMapper;

    /**
     * 关于我们列表
     *
     * @author cxt
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<AboutUsListedVo>
     */
    @Override
    public PageResult<AboutUsListedVo> list(PageValidate pageValidate, AboutUsSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<AboutUs> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        aboutUsMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:cooperation:str",
            "=:advertising:str",
            "=:customer:str",
            "=:workdayTime@workday_time:str",
            "=:holidaysTime@holidays_time:str",
            "=:address:str",
            "=:creator:str",
            "=:mender:str",
        });

        IPage<AboutUs> iPage = aboutUsMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<AboutUsListedVo> list = new LinkedList<>();
        for(AboutUs item : iPage.getRecords()) {
            AboutUsListedVo vo = new AboutUsListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 关于我们详情
     *
     * @author cxt
     * @param id 主键参数
     * @return AboutUs
     */
    @Override
    public AboutUsDetailVo detail(Integer id) {
        AboutUs model = aboutUsMapper.selectOne(
                new QueryWrapper<AboutUs>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        AboutUsDetailVo vo = new AboutUsDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 关于我们新增
     *
     * @author cxt
     * @param createValidate 参数
     */
    @Override
    public void add(AboutUsCreateValidate createValidate) {
        AboutUs model = new AboutUs();
        model.setCooperation(createValidate.getCooperation());
        model.setAdvertising(createValidate.getAdvertising());
        model.setCustomer(createValidate.getCustomer());
        model.setWorkdayTime(createValidate.getWorkdayTime());
        model.setHolidaysTime(createValidate.getHolidaysTime());
        model.setAddress(createValidate.getAddress());
        model.setCreator(createValidate.getCreator());
        model.setMender(createValidate.getMender());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        aboutUsMapper.insert(model);
    }

    /**
     * 关于我们编辑
     *
     * @author cxt
     * @param updateValidate 参数
     */
    @Override
    public void edit(AboutUsUpdateValidate updateValidate) {
        AboutUs model = aboutUsMapper.selectOne(
                new QueryWrapper<AboutUs>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setCooperation(updateValidate.getCooperation());
        model.setAdvertising(updateValidate.getAdvertising());
        model.setCustomer(updateValidate.getCustomer());
        model.setWorkdayTime(updateValidate.getWorkdayTime());
        model.setHolidaysTime(updateValidate.getHolidaysTime());
        model.setAddress(updateValidate.getAddress());
        model.setCreator(updateValidate.getCreator());
        model.setMender(updateValidate.getMender());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        aboutUsMapper.updateById(model);
    }

    /**
     * 关于我们删除
     *
     * @author cxt
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        AboutUs model = aboutUsMapper.selectOne(
                new QueryWrapper<AboutUs>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        aboutUsMapper.delete(new QueryWrapper<AboutUs>().eq("id", id));
    }

}
